﻿using System;
using System.Linq;
using System.Web;
using System.Data;
using System.Text.RegularExpressions;//引入正则表达式命名空间
using System.Text;

namespace msBsc.WebUi
{
    public partial class mfCustBought : msBsc.Cls.mbPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
            }
        }

        //private void ShowStatic()
        //{
        //    using (msBsc.AppLogic.DataAlc.CBizShpCustomerInfo odataobj = new AppLogic.DataAlc.CBizShpCustomerInfo())
        //    {
        //        DataSet ds = odataobj.SelectUserStatistic(msBsc.SvRef.UserInfo.Operator);
        //        if (ds.Tables[0].Rows.Count > 0)
        //        {
        //            this.ExecJScript("$('#emD2').html('" + ds.Tables[0].Rows[0]["WAIT_PAY_COUNT"].ToString() + "');");
        //            this.ExecJScript("$('#emD3').html('" + ds.Tables[0].Rows[0]["WAIT_SHIPPING_COUNT"].ToString() + "');");
        //            this.ExecJScript("$('#emD4').html('" + ds.Tables[0].Rows[0]["WAIT_RECEIPT_COUNT"].ToString() + "');");
        //            this.ExecJScript("$('#emD5').html('" + ds.Tables[0].Rows[0]["WAIT_RETURN_COUNT"].ToString() + "');");
        //            this.ExecJScript("$('#emD6').html('" + ds.Tables[0].Rows[0]["WAIT_REVIEW_COUNT"].ToString() + "');");
        //        }
        //    }
        //}

        private void loadlist(  string strTradeSid 
            , string CurrPageNum, string strPageRowsCount, string strSort)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpOrderForm odataobj = new AppLogic.DataAlc.CBizShpOrderForm())
            {
                string strsql = @"
    SELECT 
    A.ORDER_FORM_ID
    ,A.ORDER_FORM_NO
    ,A.ORDER_FORM_NME
    ,A.INITIAL_DT
    ,A.PAYABLE_AMT
    ,A.LOGISTICS_AMT
    ,A.TRADE_STATUS_TXT
    ,A.COMM_TENANT_NME
    ,A.COMM_TENANT_ID
    ,B.IMG_URL
    FROM SHP_ORDER_FORM A
    LEFT OUTER JOIN SHP_ORDER_FORM_LIST B ON A.ORDER_FORM_ID=B.ORDER_FORM_ID AND B.SEQ_NUM=0
    WHERE
    (A.USER_NME='" + msBsc.SvRef.UserInfo.Operator + @"') 
    ";
                if (strTradeSid != string.Empty)
                {
                    strsql += "AND (A.TRADE_STATUS_TXT='" + strTradeSid + "')";
                }
                DataSet ds = odataobj.DataObj.QueryBySQL(strsql);

                //DataSet ds = odataobj.SelectOrderForCustomFinder("", msBsc.SvRef.UserInfo.Operator, strGoodsNme, strDealerNme, strOrderNo, strStartDt, strEndDt, strTradeSid, strReviewSid, strAfterSvcSid);

                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", ""
                    , (CurrPageNum == string.Empty ? 1 : int.Parse(CurrPageNum))
                    , (strPageRowsCount == string.Empty ? 20 : int.Parse(strPageRowsCount))
                    , (strSort == string.Empty ? "INITIAL_DT DESC,ORDER_FORM_NO ASC" : strSort));
                this.ExecJScript(@" var data= " + strjsontem1 + @" ; 
                updateTableHtml( data);");
                //ShowStatic();
            }
        }

        private void doSellerReply(string strOrderListId)
        {
            if (this.GetParamHtValue("BUYER_RATE_DESC2") == string.Empty)
            {
                throw new Exception("评价内容不能为空！");
            }
            using (msBsc.AppLogic.DataAlc.CBizShpOrderRate oCDbAccess = new msBsc.AppLogic.DataAlc.CBizShpOrderRate())
            {
                DataSet ds = oCDbAccess.SelectById(strOrderListId);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["USER_NME"].ToString().ToUpper() != msBsc.SvRef.UserInfo.Operator.ToUpper())
                    {
                        throw new Exception("只能评价自己购买的订单！");
                    }
                    if (ds.Tables[0].Rows[0]["BUYER_RATE_DT2"].ToString() == string.Empty)
                    {
                        ds.Tables[0].Rows[0]["BUYER_RATE_DESC2"] = this.GetParamHtValue("BUYER_RATE_DESC2");
                        ds.Tables[0].Rows[0]["BUYER_RATE_DT2"] = System.DateTime.Now;
                        oCDbAccess.Update(ds);
                    }
                    else
                    {
                        throw new Exception("已经追加过评价，不能重复评价！");
                    }
                    this.ExecJScript("layer.close(layerform);doToPageNm();");
                }
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "doFinderBoughtList")
            {
                loadlist( this.GetParamHtValue("TRADE_STATUS_TXT") 
                    , this.GetParamHtValue("NEW_PAGE_NUM")
                    , this.GetParamHtValue("PAGE_ROW_COUNT")
                    , this.GetParamHtValue("PAGE_ROW_SORT"));
            }
            else if (this.strAction == "doSellerReply")
            {
                doSellerReply(this.GetParamHtValue("ORDER_LIST_ID"));
            }
            else if (this.strAction == "docancelorder")
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objorder = new AppLogic.DataAlc.CBizShpOrderForm())
                    {
                        objorder.CancelOrder(this.GetParamHtValue("ID"));
                        this.ExecJScript("doToPageNm();");
                    }
                    ts.Complete();
                }
            }
            else if (this.strAction == "dotipsellerdeli")
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objorder = new AppLogic.DataAlc.CBizShpOrderForm())
                    {
                        objorder.TipSellerDeli(this.GetParamHtValue("ID"));
                        this.ExecJScript("doToPageNm();");
                    }
                    ts.Complete();
                }
            }
            else if (this.strAction == "toreturncharge")
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objorder = new AppLogic.DataAlc.CBizShpOrderForm())
                    {
                        objorder.ReturnCharge(this.GetParamHtValue("ID"));
                        this.ExecJScript("doToPageNm();");
                    }
                    ts.Complete();
                }
            }
            else if (this.strAction == "dodeleteorder")
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objorder = new AppLogic.DataAlc.CBizShpOrderForm())
                    {
                        objorder.DeleteOrder(this.GetParamHtValue("ID"));
                        this.ExecJScript("doToPageNm();");
                    }
                    ts.Complete();
                }
            }
        }
    }
}